home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / asm / mpublic.com / MPUBLIC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  5.5 KB  |  209 lines

  1.  
  2. /*************************************************************************
  3.  
  4.     Mpublic.c        : Simple text parser to build "Public" statements in
  5.                           8086 Assembly language source files. This is useful
  6.                           for creating symbol/map files for debuggers. Compiled
  7.                           with Turbo C++ 1.0. Created by Craig Derouen.
  8.                           Compuserve ID 75136,1261
  9.  
  10.                           Version 2.0   6/24/90. This version parses proper
  11.                           MASM and TASM (both MASM and IDEAL modes) source files.
  12.                           It will ignore statements inside MACROS,STRUCTURES and
  13.                           local labels.
  14.  
  15.                           Most of the code is portable ANSI. The only non-standard
  16.                           stuff is the FNSPLIT & FNMERGE functions. There are
  17.                           eqvivalents for these in Microsoft C. You should be able
  18.                           to recompile by changing these & the header file names.
  19.                           All other functions are pure ANSI.
  20.  
  21.     Usage:           "Mpublic sourcefile [destfile]"
  22.  
  23.                             Sourcefile may be any standard DOS filename. No
  24.                             wildcards accepted. Destfile is optional. If not
  25.                             specified the source filename is used and '.PUB'
  26.                             extension is appended to the name.
  27.  
  28.  
  29. *************************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <alloc.h>
  33. #include <dir.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37.  
  38. #define maxlen 133
  39. #define TRUE 1
  40. #define FALSE 0
  41.  
  42. void makeparm ( FILE *, char *);
  43.  
  44. main (int argc,char *argv[])
  45. {
  46.    FILE *infile,*outfile;
  47.     char *strng,*parm1,*parm2,*parm3;
  48.     char temp[81];
  49.     int mflag=FALSE,i;
  50.     int locallab = FALSE;
  51.     int linecnt=1;
  52.     char drive[MAXDRIVE];
  53.     char dir[MAXDIR];
  54.     char file[MAXFILE];
  55.     char ext[MAXEXT];
  56.  
  57.     printf("MPUBLIC version 2.0, by Craig Derouen\n");
  58.  
  59.     if (argc <2) {
  60.        printf ("\007Sorry you must enter a valid filename\n");
  61.         exit(1);
  62.     }
  63.     if ( (infile = fopen(argv[1],"r")) == NULL ) {
  64.        printf ("\007Sorry,bad filename\n");
  65.         exit(1);
  66.     }
  67.     if (argc < 3) {
  68.     /* No output file specified. Create output file from input filename */
  69.         strcpy(temp,argv[1]); // Copy name to work with
  70.         i = fnsplit(temp,drive,dir,file,ext);
  71.         strcpy(ext,".PUB");
  72.         fnmerge(temp,drive,dir,file,ext);
  73.         if ( (outfile = fopen(temp,"w+")) == NULL ) {
  74.             printf ("\007Sorry, could not open output filename\n");
  75.             exit(1);
  76.         }
  77.     }
  78.     else {
  79.         if ( (outfile = fopen(argv[2],"w+")) == NULL ) {
  80.             printf ("\007Sorry, could not open output filename\n");
  81.             exit(1);
  82.         }
  83.     }
  84.     strng = (char *) malloc(maxlen+1);
  85.     if (strng == NULL) {
  86.         printf("\aSorry, can not allocate enough memory.\n");
  87.         exit(1);
  88.     }
  89.  
  90.     while (! feof(infile)) {
  91.         fgets(strng,maxlen,infile);
  92.         printf("Processing line number# %d\r",linecnt++);
  93. /* Strip leading whitespace */
  94.       while (isspace(*strng))
  95.             ++strng;
  96.         parm1 = strtok(strng," \t\r\n");
  97.         parm2 = strtok(NULL," \t\r\n");
  98.  
  99. /* Now that we have tokens, parse them out for various types of labels
  100. *    and defines */
  101.  
  102.         if (!mflag) { // Not inside macro or structure
  103.  
  104.             if (strcmpi(parm1,"LOCALS") == 0)  // Turn on local labels in Tasm
  105.                 locallab = TRUE;
  106.  
  107.             if (strcmpi(parm1,"IDEAL") == 0) // TASM IDEAL mode
  108.                 locallab = TRUE;
  109.  
  110.             if (strcmpi(parm2,"STRUC") == 0 )
  111.                 mflag = TRUE; // Start of structure
  112.  
  113.             if (strcmpi(parm2,"MACRO") == 0 )
  114.                 mflag = TRUE; // Start of macro
  115.  
  116.             if (strcmpi(parm1,"@@:") == 0)  // MASM style local label
  117.                 continue; // Try next line
  118.  
  119.             i = strlen(parm1); // Look at tail of token
  120.             if (parm1[i-1] == ':') { // Is it a code label?
  121.                 parm1[i-1] = '\0'; // Erase colon
  122.                 if (locallab) { // Are TASM local lables turned on?
  123.                     if (strnicmp(parm1,"@@",2) == 0)
  124.                         continue; // TASM local lables have a '@@' prepended
  125.                 }
  126.                 makeparm(outfile,parm1);
  127.                 continue;
  128.             }
  129.  
  130.             if (strcmpi(parm2,"PROC") == 0) { // Code label
  131.                 makeparm(outfile,parm1);
  132.                 continue;
  133.             }
  134.  
  135.             if (strcmpi(parm2,"LABEL") == 0) {
  136.                 makeparm(outfile,parm1);
  137.                 continue;
  138.             }
  139.  
  140.             if (strcmpi(parm2,"DB") == 0) { // Data label
  141.                 makeparm(outfile,parm1);
  142.                 continue;
  143.             }
  144.  
  145.             if (strcmpi(parm2,"DW") == 0) { // Data label
  146.                 makeparm(outfile,parm1);
  147.                 continue;
  148.             }
  149.  
  150.             if (strcmpi(parm2,"DD") == 0) { // Data label
  151.                 makeparm(outfile,parm1);
  152.                 continue;
  153.             }
  154.  
  155.             if (strcmpi(parm2,"DQ") == 0) { // Data label
  156.                 makeparm(outfile,parm1);
  157.                 continue;
  158.             }
  159.  
  160.             if (strcmpi(parm2,"DT") == 0) { // Data label
  161.                 makeparm(outfile,parm1);
  162.                 continue;
  163.             }
  164.  
  165.             if (strcmpi(parm2,"DF") == 0) { // Data label
  166.                 makeparm(outfile,parm1);
  167.                 continue;
  168.             }
  169.         }
  170.         else { // We are inside a structure/macro. Look for end
  171.             if (strcmpi(parm2,"ENDS") == 0) // End of structure definition
  172.                 mflag = FALSE;
  173.  
  174.             if (strcmpi(parm1,"ENDM") == 0) // End of macro
  175.                 mflag = FALSE;
  176.         }
  177.     }
  178.     printf("\n");
  179.     fclose(infile);
  180.     fclose(outfile);
  181.     return(0);
  182. }
  183.  
  184. void makeparm (outfile,parm)
  185. FILE *outfile;
  186. char *parm;
  187. {
  188.     static linelen = 0;
  189.     int i;
  190.  
  191.     if(!linelen) { // Starting over
  192.         fprintf(outfile,"PUBLIC        ");
  193.         linelen = 15; // Track column pos of line. Don't exceed 78 columns
  194.     }
  195.  
  196.     i = strlen(parm);
  197.  
  198.     if ((i + linelen) > 78) { // Start over on new line
  199.         fprintf(outfile,"\n"); // Close out line and start newone
  200.         fprintf(outfile,"PUBLIC        ");
  201.         linelen = 15; // Track column pos of line. Don't exceed 78 columns
  202.     }
  203.     if (linelen == 15) // First parm on line?
  204.         fprintf(outfile,"%s",parm); // Don't put comma on leading lable
  205.     else fprintf(outfile,",%s",parm);
  206.     linelen += 1 + strlen(parm);
  207.     return;
  208. }
  209.